package JF;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Test {

	public static void main(String[] args) {

		System.out.println(login("admin", "123456"));

	}

	public static boolean login(String username, String password) {
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String url = "jdbc:sqlserver://"// 统一规范
				+ "192.168.219.55"// 哪个主机的数据库
				+ ":1433"// 哪个端口
				+ ";DatabaseName=WordBase"; // 哪个数据库
		String userName = "sa";
		String userPwd = "Thanks2015";

		try {
			try {
				Class.forName(driverName);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
			Connection conn = DriverManager.getConnection(url, userName,
					userPwd);
			String sql = "select count(*) count from t_user where username = ? and password = ?";

			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setString(1, username);
			psmt.setString(2, password);

			ResultSet rs = psmt.executeQuery();
			if (rs != null && rs.next())
				return rs.getInt("count") > 0;
			else
				return false;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public static boolean regist(String username, String password) {
		String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		String url = "jdbc:sqlserver://"// 统一规范
				+ "192.168.219.55"// 哪个主机的数据库
				+ ":1433"// 哪个端口
				+ ";DatabaseName=WordBase"; // 哪个数据库
		String userName = "sa";
		String userPwd = "Thanks2015";

		try {
			try {
				Class.forName(driverName);
			} catch (ClassNotFoundException e) {

				e.printStackTrace();
			}
			Connection conn = DriverManager.getConnection(url, userName,
					userPwd);
			String sql = "insert into t_user(username,password) values(?,?)";

			PreparedStatement psmt = conn.prepareStatement(sql);
			psmt.setString(1, username);
			psmt.setString(2, password);

			return psmt.executeUpdate() > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}